﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObligatorioBD.MerObjects
{
    public class Mer : IObjectId
    {
        List<Entity> entities;
        List<Relationship> relations;
        String name;
        String owner;
        int id = -1;
        public Mer(string name, string owner = "")
        {
            this.name = name;
            this.owner = owner;
            this.entities = new List<Entity>();
            this.relations = new List<Relationship>();

        }
        public Mer(int merId, string name, string owner = "") : this(name, owner)
        {
            this.id = merId;
        }
        public void Delete()
        {
            Logic.MerPersistor mPersistor = new Logic.MerPersistor();
            mPersistor.DeleteMer(this);
        }
        public void DeteleRelation(Relationship r)
        {
            this.relations.Remove(r);
        }
        public Entity GetEntityById(int id)
        {
            Entity e = null;
            foreach (Entity ent in this.entities)
            {
                if (ent.ObjectId == id)
                {
                    e = ent;
                }
            }
            return e;
        }
        public string Owner
        {
            get { return this.owner;}
        }
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
        public List<Entity> GetEntities()
        {
            return this.entities;
        }
        public List<Relationship> GetRelations()
        {
            return this.relations;
        }
        public void AddEntity(Entity e)
        {
            this.entities.Add(e);
        }
        public void AddRelation(Relationship r)
        {
            this.relations.Add(r);
        }
        public Entity GetEntityByName(string name)
        {
            Entity ent = null;
            foreach (Entity e in this.entities)
            {
                if (e.Name.ToUpper() == name.ToUpper())
                {
                    ent = e;
                }
            }
            return ent;
        }
        public int ObjectId
        {
            get { return this.id; }
            set { this.id = value; }
        }
    }
}
